package zsc.nine.onlinemall.controller;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import zsc.nine.onlinemall.service.AdminService;
import zsc.nine.onlinemall.service.UserService;
import zsc.nine.onlinemall.utils.SendMailTool;

/**
 * @ClassName: MailController
 * @Description: Controller of sending QQ-Mail
 * @Author: Zainus
 * @CreateDate: Jul.20, 2021
 * @UpdateDate: Jul.20, 2021
 * @Version: [v1.0]
 */

/**
 * @类名: MailController
 * @描述: 发送邮件工具接口
 * @创建人: 叶增钥
 * @创建时间: 2021年7月20日
 * @更新时间: 2021年7月20日
 * @版本: [v1.0]
 */

@RequestMapping("/Mail")
@RestController
public class MailController {

    /**
     * 自动注入发送邮件工具类
     */
    @Autowired
    SendMailTool sendMailTool;

    private final UserService userService;
    private final AdminService adminService;

    public MailController(UserService userService, AdminService adminService) {

        this.userService = userService;
        this.adminService = adminService;

    }


    /**
     * @param UserEmail: 用户邮箱地址
     * @param Type:      验证类型 0为注册 1为找回密码 2为更改邮箱
     * @param UserName:  用户名
     * @Description: 发送邮箱返回验证码的方法
     * @return: 发送结果
     * 操作结果： 一用JSONString的CaptchaResult字段携带，Parameter-Null为传入参数缺失，Empty为后端判断数据为空，
     * Existed为注册Email已存在，Wrong-Email为重置密码Email不存在，Fail为失败，Success为成功
     * 二生成验证码用JSONString的Captcha字段携带
     */
    @PostMapping("/Captcha")
    public String Captcha(@RequestParam(value = "UserEmail", required = false) String UserEmail,
                          @RequestParam(value = "Type", required = false) String Type,
                          @RequestParam(value = "UserName", required = false) String UserName) {

        /**
         * 创建用于返回结果的Json对象，并初始化CaptchaResult为Fail
         */
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("CaptchaResult", "Fail");

        /**生成随机验证码*/
        String randomCaptcha = RandomStringUtils.random(6, "0123456789");
        /**后端校验数据合法性*/
        if (UserEmail == null
                || Type == null
                || (Type.equals("0") && UserName == null)
        ) {

            jsonObject.put("CaptchaResult", "Parameter-Null");
        } else if (UserEmail.trim().isEmpty()
                || Type.trim().isEmpty()
                || ((Type.equals("0") || Type.equals("2")) && UserName.trim().isEmpty())
        ) {
            jsonObject.put("CaptchaResult", "Empty");
        }

        /**后端判断Email是否已被占用*/
        else if (
                (Type.equals("0") || Type.equals("2"))
                        && (userService.UserExist(UserEmail) || (adminService.AdminExit(UserEmail)))
        ) {

            jsonObject.put("CaptchaResult", "Existed");
        }

        /**后端校验Email是否存在*/
        else if (Type.equals("1") && !userService.UserExist(UserEmail)) {
            jsonObject.put("CaptchaResult", "Wrong-Email");
        }

        /**调用SendMailTool工具类发送邮件，成功则将CaptchaResult字段置为Success*/
        else {

            /**判断是否重置密码时用户名为空*/
            if (Type.equals("1") && (UserName == null || UserName.trim().isEmpty())) {
                UserName = userService.findUserByEmail(UserEmail).getUserName();
            }

            if (sendMailTool.sendCatcha(
                    randomCaptcha,
                    Type,
                    UserEmail,
                    UserName)) {
                jsonObject.put("CaptchaResult", "Success");
                jsonObject.put("Captcha", randomCaptcha);
            }

        }


        return jsonObject.toJSONString();

    }

    /**
     * @param UserEmail: 用户邮箱地址
     * @param Type:      验证类型 0为发货 1为退款
     * @param UserName:  用户名
     * @Description: 发送服务邮件的方法
     * @return: 发送结果
     * 操作结果： 用JSONString的CaptchaResult字段携带，Parameter-Null为传入参数缺失，Empty为后端判断数据为空，
     * Wrong-Email为Email不存在，Fail为失败，Success为成功
     */
    @PostMapping("/ServeMail")
    public String ServeMail(@RequestParam(value = "UserEmail", required = false) String UserEmail,
                            @RequestParam(value = "Type", required = false) String Type,
                            @RequestParam(value = "UserName", required = false) String UserName) {

        /**
         * 创建用于返回结果的Json对象，并初始化CaptchaResult为Fail
         */
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("ServeMailResult", "Fail");

        /**后端校验数据合法性*/
        if (UserEmail == null
                || Type == null
        ) {

            jsonObject.put("ServeMailResult", "Parameter-Null");
        } else if (UserEmail.trim().isEmpty()
                || Type.trim().isEmpty()
        ) {
            jsonObject.put("ServeMailResult", "Empty");
        }

        /**后端校验Email是否存在*/
        else if (Type.equals("1") && !userService.UserExist(UserEmail)) {
            jsonObject.put("ServeMailResult", "Wrong-Email");
        }

        /**调用SendMailTool工具类发送邮件，成功则将CaptchaResult字段置为Success*/
        else {

            /**判断是否重置密码时用户名为空*/
            if (UserName == null || UserName.trim().isEmpty()) {
                UserName = userService.findUserByEmail(UserEmail).getUserName();
            }

            if (sendMailTool.sendMail(
                    Type,
                    UserEmail,
                    UserName)) {
                jsonObject.put("ServeMailResult", "Success");
            }

        }


        return jsonObject.toJSONString();

    }

}
